# @before-stub-for-debug-begin
from python3problem213 import *
from typing import *
# @before-stub-for-debug-end

#
# @lc app=leetcode.cn id=213 lang=python3
#
# [213] 打家劫舍 II
#

# @lc code=start
class Solution:
    def rob(self, nums: List[int]) -> int:
     
        n = len(nums)
        if n==1:
            return nums[0]
        def robs(nums):
            n = len(nums)
            dp = [0 for i in range(n)]
            dp[0] = nums[0]
            for i in range(1,n):
                dp[i] = max(dp[i-1],dp[i-2] + nums[i])
            return dp[n-1]
        return max(robs(nums[1:]), robs(nums[:-1]))
# @lc code=end

